package com.wyy.servlet.web;

import com.wyy.bean.Menu;
import com.wyy.bean.Role;
import com.wyy.service.MenuService;
import com.wyy.service.RoleService;
import com.wyy.service.impl.MenuServiceImpl;
import com.wyy.service.impl.RoleServiceImpl;
import com.wyy.util.PageUtil;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.io.PrintWriter;
import java.util.List;

/**
 * @ClassName RoleServlet
 * @Description //TODO
 * @Author wky
 * @Date 2021/9/28 19:05
 * @Version 1.0
 **/
@WebServlet(urlPatterns = "/power/role/roles")
public class RoleServlet extends HttpServlet {
    private RoleService roleService = new RoleServiceImpl();
    private MenuService menuService = new MenuServiceImpl();
    @Override
    protected void service(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        String method = req.getParameter("method");
        if ("selectMenus".equals(method)){
            selectMenus(req, resp);
        }else if ("insert".equals(method)){
            insert(req, resp);
        }else if("delete".equals(method)){
            delete(req,resp);
        }else if ("findById".equals(method)){
            findById(req,resp);
        }else if ("update".equals(method)){
            update(req,resp);
        }else{
            select(req,resp);
        }
    }
    
    protected void select(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        //1.接收参数(分页参数+模糊查参数)
        String index = req.getParameter("index");
        int pageIndex=(index==null||index.length()==0)?1:Integer.parseInt(index);
        //2.调取service方法(1.查询数据列表的方法  2.查询总条数的方法)
        PageUtil pageUtil = new PageUtil();
        List<Role> roleList = roleService.getRoleList(pageIndex, pageUtil.getPageSize());
        int total = roleService.total();
        pageUtil.setTotal(total);
        pageUtil.setPageIndex(pageIndex);
        pageUtil.setList(roleList);
        //3.存值跳页面
        req.setAttribute("pi",pageUtil);
        req.getRequestDispatcher("list.jsp").forward(req,resp);
    }
    
    protected void selectMenus(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException  {
        //接收参数
        //调取service方法
        List<Menu> menuList = menuService.getMenuList();
        req.setAttribute("menuList",menuList);
        //跳转页面
        req.getRequestDispatcher("add.jsp").forward(req,resp);
    }
    
    protected void insert(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        //接收参数
        String roleName = req.getParameter("roleName");
        String[] menuId = req.getParameterValues("menuId");
        String state = req.getParameter("state");
        //调取service方法
        int i = roleService.insertRole(roleName, state, menuId);
        //跳转页面
        if (i>0){
            req.getRequestDispatcher("/power/role/roles?method=selected").forward(req,resp);
        }else{
            req.getRequestDispatcher("/power/role/roles?method=selectMenus").forward(req,resp);
        }

    }

    protected void delete(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        String roleId = req.getParameter("roleId");
        int res = roleService.deleteRole(Integer.parseInt(roleId));

        //转发页面
        resp.setContentType("text/html;charset=utf-8");
        PrintWriter writer = resp.getWriter();
        if (res>0){
            writer.println("<script>alert('删除成功');location.href='/power/role/roles?method=selected'</script>");
        }else{
            writer.println("<script>alert('删除失败');location.href='/power/role/roles?method=selected'</script>");
        }

    }

    protected void findById(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        String roleId = req.getParameter("roleId");
        System.out.println("roleId----"+roleId);
        //调用service
        Role role = roleService.findById(Integer.parseInt(roleId));
        List<Menu> menuList = menuService.getMenuList();
        System.out.println("role------"+role.getRoleId()+role.getRoleName());
        System.out.println("menu------"+menuList.get(0).getMenuName());
        //转发页面
        req.setAttribute("role",role);
        req.setAttribute("menuList",menuList);
        req.getRequestDispatcher("edit.jsp").forward(req,resp);
    }

    protected void update(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        String roleId = req.getParameter("roleId");
        String roleName = req.getParameter("roleName");
        String state = req.getParameter("state");
        String[] menuIds = req.getParameterValues("menuId");
        Role role = new Role();
        role.setRoleId(Integer.parseInt(roleId));
        role.setRoleName(roleName);
        role.setRoleState(Integer.parseInt(state));
        int update = roleService.updateRole(role, menuIds);
        //跳转页面
        resp.setContentType("text/html;charset=utf-8");
        PrintWriter writer = resp.getWriter();
        if (update>0){
            writer.println("<script>alert('修改成功');location.href='/power/role/roles?method=selected'</script>");
        }else{
            writer.println("<script>alert('修改失败');location.href='/power/role/roles?method=findById&roleId = "+roleId+"'</script>");
        }
    }
}

